<template>
  <div>
    obj : <input v-model="obj.count" /> X: <input v-model="x" /> Y:
    <input v-model="y" />
    {{ cx }}
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
const x = ref(0)
const y = ref(1)
const obj = ref({ count: 0 })

watch(
  obj,
  () => {
    console.log(`OBJ== Count is: ${obj.value}`)
  },
  // 只要改变对象的任意一个属性的值，不需要改变对象本身，就可以触发监听器
  { deep: true }
)

watch(
  () => obj.value.count,
  () => {
    console.log(`Count is: ${obj.value.count}`)
  }
)

// 错误，因为 watch() 得到的参数是一个 number
// 监听器默认是浅层监听
watch(obj.value.count, (count) => {
  console.log(`Count is: ${count}`)
})

watch(
  () => x.value + y.value,
  (newVal, oldVal) => {
    console.log('new =' + newVal)
    console.log(oldVal)
  }
)

watch(x, (nx, ox) => {
  console.log('new = ' + nx)
  console.log(ox)
})

const cx = computed(() => {
  console.log(x.value)
  return x.value + x.value
})
</script>

<style lang="scss" scoped></style>
